<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2020/7/18
  Time: 22:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<jsp:include page="../common/menu.jsp" />

<html>
<head>
    <title>车辆信息管理</title>
</head>

<script src="<%=basePath%>js/tools.js"></script>
<body>
<div class="layui-tab">
    <ul class="layui-tab-title">
        <li class="layui-this">驾驶员管理</li>
    </ul>
    <div class="layui-tab-content">
        <table class="layui-hide" id="backUser" lay-filter="backUser"></table>
    </div>
</div>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addUser">新增数据</button>
    </div>
</script>

<script type="text/html" id="bduCreateDate">
    {{ dateFormat(d.createTime) }}
</script>

<script type="text/html" id="bpilotLicenseTime">
    {{ dateFormat(d.pilotLicenseTime) }}
</script>
<script type="text/html" id="bworkLicenseTime">
    {{ dateFormat(d.workLicenseTime) }}
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="启用|停用" lay-filter="bduStatusFilter" {{ d.status == 1 ? 'checked' : '' }}>
</script>
</body>
</html>
<jsp:include page="../common/js.jsp" />
<script type="text/javascript">

    $('document').ready(function() {
        var layer = null;
        var laypage = null;
        var table = null;
        var form = null;
        layui.use([ "laypage", "layer", 'table' ], function() {
            layer = layui.layer;
            laypage = layui.laypage;
            table = layui.table;
            form = layui.form;

            table.render({
                elem : '#backUser',
                url : '<%=basePath%>pilot/pilotList',
                id : 'backUser',
                height:650,
                toolbar : '#toolbar',
                cellMinWidth : 100,
                cols : [ [
                     {
                        field : 'id',
                        title : '编号',
                        width : '5%',
                        unresize : true,
                        sort : true
                    }
                    , {
                        field : 'pilotName',
                        title : '驾驶员姓名',
                        width : '10%',
                        sort : true
                    }
                    , {
                        field : 'pilotCrad',
                        title : '身份证号码',
                        edit: 'text',
                        width : '10%',
                        unresize : true
                    }
                    , {
                        field : 'pilotTel',
                        title : '联系电话',
                        width : '10%',
                        edit: 'text',
                        sort : true
                    }
                    , {
                        field : 'pilotAdr',
                        title : '联系地址',
                        width : '10%',
                        edit: 'text',
                        sort : true
                    }
                    , {
                        field : 'pilotLicenseTime',
                        title : '驾驶证有效期',
                        width : '10%',
                        templet : '#bpilotLicenseTime',
                        sort : true
                    }
                    , {
                        field : 'workLicenseTime',
                        title : '上岗证有效期',
                        width : '10%',
                        templet : '#bworkLicenseTime',
                        sort : true
                    }
                    , {
                        field : 'remark',
                        title : '备注',
                        edit: 'text',
                        width : '12%',
                        unresize : true
                    }
                    , {
                        field : 'createBy',
                        title : '创建人',
                        width : '10%',
                        sort : true
                    }
                    , {
                        field : 'createtime',
                        title : '创建日期',
                        width : '10%',
                        templet : '#bduCreateDate',
                        unresize : true
                    }
                    , {
                        fixed : 'right',
                        width : '10%',
                        align : 'center',
                        toolbar : '#status',
                        title : '状态'
                    }
                ] ],
                page : true
            });

            //监听单元格编辑 TODO
            table.on('edit(backUser)', function(obj){
                var value = obj.value //得到修改后的值
                    ,data = obj.data //得到所在行所有键值
                    ,field = obj.field; //得到字段
                layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);

                var postData ={};
                postData["id"] = data.id;
                postData[field] = obj.value;

                $.ajax({
                    url:'<%=basePath %>pilot/updatePilotByPk',
                    data:postData,
                    type:'POST',
                    success:function(data){
                        if(data.responseHead.code == '200'){
                            layer.alert('操作成功!', {icon: 1,title: "结果"});
                        }else{
                            layer.alert(data.responseHead.msg, {icon: 5,title: "结果"});
                        }

                        table.reload('backUser', {
                            url: '<%=basePath%>pilot/pilotList'
                        });
                    }
                });
            });

            //头工具栏事件
            table.on('toolbar(backUser)', function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch(obj.event){
                    case 'addUser':
                        layer.msg('添加');
                        window.location.href="<%=basePath %>pilot/toAddPilot";
                        break;
                };

            });

            form.on('switch(bduStatusFilter)', function(obj) {

                var ids =[];
                ids[0] = this.id;
                var type = this.value=="1"?0:1;
                $.ajax({
                    url:'<%=basePath %>pilot/updatePilotStatus',
                    data:{
                        "type":type,
                        "list":ids
                    },
                    type:'POST',
                    success:function(data){
                        if(data.responseHead.code == '200'){
                            layer.alert('操作成功!', {icon: 1,title: "结果"});
                        }else{
                            layer.alert(data.responseHead.msg, {icon: 5,title: "结果"});
                        }

                        table.reload('backUser', {
                            url: '<%=basePath%>pilot/pilotList'
                        });
                    }
                });
            });

        });
    });
</script>
